package com.mawc.equipower.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.mawc.equipower.model.PaserMode;
import lombok.Data;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PaserExcel
 * @ProjectName equippower
 * @Description
 * @Author 马文才
 * @Date 2021.10.30 10:23
 */
@Data
public class PaserExcel {
    private String path;
    private List<PaserMode> paserModes;
    private String data;

    public PaserExcel(String path,String dataPath) {
        this.path = path;
        getData(dataPath);
        getList();
    }

    private void getList() {
        ExcelReader reader = ExcelUtil.getReader(path);
        List<PaserMode> all = reader.readAll(PaserMode.class);
        all.sort((o1,o2)-> o1.getIndex()-o2.getIndex());                       //excel表中每行是一个paserMode

        Integer start=0;
        Integer c=0;
        for (PaserMode paserMode : all) {
            paserMode.setContent(paserMode.getContent().replaceAll(" ",""));
            String content = paserMode.getContent();
            if(content.length()==1){
                content="0"+content;
            }
            paserMode.setContent(content);
            paserMode.setCount(paserMode.getContent().length());
            Integer end=(paserMode.getCount()+start);
            c+=paserMode.getCount();
            paserMode.setLocation(start+"-"+end);
            start+=paserMode.getCount();


        }                                                                 //for循环操作excel表中content字段
        System.out.println("总数"+c);
        this.paserModes = all;
    }

    public static void main(String[] args) {
//        PaserExcel paserExcel = new PaserExcel("E:\\download\\解析协议.xls");
//        System.out.println(paserExcel.paserModes);
    }


    /**
     * 读取数据,按照位置数对数据进行解析
     */
    public void getData(String textpath){
        try {
            File dataFile = ResourceUtils.getFile("classpath:"+textpath);
           /* BufferedReader bufferedReader = new BufferedReader(new FileReader(dataFile));
            StringBuilder stringBuilder = new StringBuilder();
            String s = bufferedReader.readLine();
            stringBuilder.append(s);
            while (s != null) {
                s = bufferedReader.readLine();
                if (s != null) {
                    stringBuilder.append(s);
                }
            }*/
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(dataFile),"UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
            data = stringBuilder.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**将数据翻译*/
    public   List<PaserMode> translationData(){
        ArrayList<PaserMode> paserModes = new ArrayList<>();
        this.data=data.replaceAll(" ","");
        for (PaserMode paserMode : this.paserModes) {
            PaserMode ts=getTranse(paserMode);
            if(ts!=null){
                paserModes.add(ts);
            }
        }
        return paserModes;
    }

    private PaserMode getTranse(PaserMode paserMode) {

        String location = paserMode.getLocation();
        String[] split = location.split("-");
        String substring = data.substring(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
        if(substring.equals(paserMode.getContent())){
            return paserMode;
        }
        return null;
    }
}
